package com.example.customizeview.viewgroups;

import android.Manifest;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

public class HorizontalView extends ViewGroup {

    private int lastX;
    private int lastY;
    private int currentIndex;
    private int childWidth;
    private Scroller scroller;
    private VelocityTracker tracker;
    private int lastInterceptX = 0;
    private int lastInterceptY = 0;

    public HorizontalView(Context context) {
        super(context);
        init();
    }

    public HorizontalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HorizontalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init(){
        scroller = new Scroller(getContext());
        tracker = VelocityTracker.obtain();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        boolean intercept = false;
        int X = (int) event.getX();
        int Y = (int) event.getY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                intercept = false;
                if(!scroller.isFinished()){
                    scroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int deltax = X - lastInterceptX;
                int deltay = Y - lastInterceptY;
                if(Math.abs(deltax)- Math.abs(deltay)>0){
                    intercept = true;
                    Log.i("print","intercepture == true");
                }else{
                    intercept = false;
                    Log.i("print","intercept == false");
                }
                break;
                case MotionEvent.ACTION_UP:
                    intercept = false;
                    break;
        }
        lastX = X;
        lastY = Y;
        lastInterceptX = X;
        lastInterceptY = Y;
        return intercept;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        tracker.addMovement(event);
        int x = (int)event.getX();
        int y = (int)event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(!scroller.isFinished()){
                    scroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int deltax = x - lastX;
                scrollBy(-deltax,0);
                break;
            case MotionEvent.ACTION_UP:
                int distance = getScrollX() - currentIndex * childWidth;
                if(Math.abs(distance)>childWidth/2){
                    if(distance>0){
                        currentIndex ++;
                    }else{
                        currentIndex --;
                    }
                }else{
                    tracker.computeCurrentVelocity(1000); //1秒内运动了多少个像素单位
                    float xV = tracker.getXVelocity();
                    if(Math.abs(xV)>50){
                        if(xV>0){
                            currentIndex--;
                        }else{
                            currentIndex++;
                        }
                    }
                }
                currentIndex = currentIndex<0?0:currentIndex>getChildCount()-1?getChildCount()-1:currentIndex;
                smoothScrollerTo(currentIndex * childWidth,0);
                tracker.clear();
                break;
                default:
                    break;
        }
        lastX = x;
        lastY = y;
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        measureChildren(widthMeasureSpec,heightMeasureSpec);
        if(getChildCount()==0){
            setMeasuredDimension(0,0);
        }else if(widthMode==MeasureSpec.AT_MOST&&heightMode==MeasureSpec.AT_MOST){
            View view = getChildAt(0);
            int childWidth = view.getMeasuredWidth();
            int childHeight = view.getMeasuredHeight();
            setMeasuredDimension(childWidth*getChildCount(),childHeight);
        }else if(widthMode == MeasureSpec.AT_MOST){
            View childone = getChildAt(0);
            int childWidth = childone.getMeasuredWidth();
            setMeasuredDimension(childWidth*getChildCount(),heightSize);
        }else if(heightMode == MeasureSpec.AT_MOST){
            int childHeight = getChildAt(0).getMeasuredHeight();
            setMeasuredDimension(widthSize,childHeight);
        }
    }

    @Override
    public void computeScroll() {
        super.computeScroll();

        if(scroller.computeScrollOffset()){
            scrollTo(scroller.getCurrX(),scroller.getCurrY());
            postInvalidate();
        }
    }

    public void smoothScrollerTo(int destX,int destY){
        scroller.startScroll(getScrollX(),getScrollY(),destX-getScrollX(),destY-getScrollY(),1000);
        invalidate();
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int childCount = getChildCount();
        int left = 0;
        View child;
        for(int a=0;a<childCount;a++){
            child = getChildAt(a);
            if(child.getVisibility()!=View.GONE){
                int width = child.getMeasuredWidth();
                childWidth = width;
                child.layout(left,0 ,left+width,child.getMeasuredHeight());
                left+=width;
            }
        }
    }
}
